﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace Punto_de_Venta
{
    public partial class Reportes : Form
    {
        public Reportes()
        {
            InitializeComponent();
        }

        private void btnVolver_Click(object sender, EventArgs e)
        {
            Principal_SPV principal = new Principal_SPV();
            principal.Show();
            Dispose();
            Close();
        }

        private void btnGeneral_Click(object sender, EventArgs e)
        {
            ReporteGeneral reporte = new ReporteGeneral();
            DatosTableAdapters.VentasTableAdapter adaptador = new DatosTableAdapters.VentasTableAdapter();
            Datos.VentasDataTable tabla = adaptador.GetData();
            reporte.SetDataSource(tabla.DefaultView);
            crvVisor.ReportSource = reporte;
            crvVisor.Refresh();
        }

        private void btnMensual_Click(object sender, EventArgs e)
        {
            DateTime fechaf = DateTime.Now;
            string fechai = "01/" + fechaf.Month + "/" + fechaf.Year;

            ReporteMensual reporte = new ReporteMensual();
            DatosTableAdapters.VentasTableAdapter adaptador = new DatosTableAdapters.VentasTableAdapter();
            Datos.VentasDataTable tabla = adaptador.GetData();
            reporte.SetDataSource(tabla.DefaultView);

            ParameterFieldDefinitions crPFDs;
            ParameterFieldDefinition crPFD;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = fechai;
            crPFDs = reporte.DataDefinition.ParameterFields;
            crPFD = crPFDs["fechai"];
            crParameterValues = crPFD.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crPFD.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = fechaf;
            crPFDs = reporte.DataDefinition.ParameterFields;
            crPFD = crPFDs["fechaf"];
            crParameterValues = crPFD.CurrentValues;

            crParameterValues.Add(crParameterDiscreteValue);
            crPFD.ApplyCurrentValues(crParameterValues);

            crvVisor.ReportSource = reporte;
            crvVisor.Refresh();
        }

        private void btnSemanal_Click(object sender, EventArgs e)
        {
            DateTime lunes = DateTime.Now;
            while (lunes.DayOfWeek != DayOfWeek.Monday)
            {
                lunes = lunes.AddDays(-1);
            }
            ReporteSemanal reporte = new ReporteSemanal();
            DatosTableAdapters.VentasTableAdapter adaptador = new DatosTableAdapters.VentasTableAdapter();
            Datos.VentasDataTable tabla = adaptador.GetData();
            reporte.SetDataSource(tabla.DefaultView);

            ParameterFieldDefinitions crPFDs;
            ParameterFieldDefinition crPFD;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = lunes;
            crPFDs = reporte.DataDefinition.ParameterFields;
            crPFD = crPFDs["fechai"];
            crParameterValues = crPFD.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crPFD.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = DateTime.Now;
            crPFDs = reporte.DataDefinition.ParameterFields;
            crPFD = crPFDs["fechaf"];
            crParameterValues = crPFD.CurrentValues;

            crParameterValues.Add(crParameterDiscreteValue);
            crPFD.ApplyCurrentValues(crParameterValues);

            crvVisor.ReportSource = reporte;
            crvVisor.Refresh();
        }

        private void btnDiario_Click(object sender, EventArgs e)
        {
            ReporteDiario reporte = new ReporteDiario();
            DatosTableAdapters.VentasTableAdapter adaptador = new DatosTableAdapters.VentasTableAdapter();
            Datos.VentasDataTable tabla = adaptador.GetData();
            reporte.SetDataSource(tabla.DefaultView);

            ParameterFieldDefinitions crPFDs;
            ParameterFieldDefinition crPFD;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = dtpFecha.Value;
            crPFDs = reporte.DataDefinition.ParameterFields;
            crPFD = crPFDs["fechai"];
            crParameterValues = crPFD.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crPFD.ApplyCurrentValues(crParameterValues);


            crvVisor.ReportSource = reporte;
            crvVisor.Refresh();
        }

    }
}
